package com.sugon.modules.kpi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sugon.modules.analyse.consts.MoldKpiTarget;
import com.sugon.modules.analyse.model.BarChartOptionModel;
import com.sugon.modules.analyse.model.BarChartSeriesModel;
import com.sugon.modules.analyse.model.vo.MoldKpiStatParamVO;
import com.sugon.modules.kpi.service.IMoldKpiTargetComputor;
import com.sugon.modules.mold.model.dto.MoldYearlyFaultStatDTO;
import com.sugon.modules.mold.service.IMoldRepairRecordManager;
import com.sugon.modules.report.service.IMoldKpiComputeManager;
import com.sugon.modules.utils.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @author ： YuXD
 * @description： 模具 MTBF计算器实现类
 * @date ： 2021-01-18 10:45
 * @version: 1.0
 */
@Service
public class MoldMTBFKPIComputorImpl implements IMoldKpiTargetComputor {

    @Autowired
    private IMoldKpiComputeManager moldKpiComputeManager;
    @Autowired
    private IMoldRepairRecordManager moldRepairRecordManager;

    @Override
    public BarChartOptionModel compute(MoldKpiStatParamVO kpiStatParam) {
        BarChartOptionModel<Double> chartOptionModel = new BarChartOptionModel<>();
        // 设置legendData
        chartOptionModel.setLegendData(kpiStatParam.getMoldNo());
        // 获取模具编码列表
        List<String> moldCodeList = kpiStatParam.getMoldCode();
        for (int i = 0; i < moldCodeList.size(); i++) {
            chartOptionModel.addBarSeriesData(compute(kpiStatParam.getYearly(), moldCodeList.get(i), kpiStatParam.getMoldNo().get(i)));
        }
        return chartOptionModel;
    }


    private BarChartSeriesModel compute(Integer statYearly, String moldCode, String moldNo) {
        BarChartSeriesModel<Double> chartSeriesModel = new BarChartSeriesModel<>(moldNo);
        // 获取模具指定年度下不同月份的生产时间
        Map<Integer, Double> yearlyMoldTimesStat = moldKpiComputeManager.doYearlyMoldProductTimeStat(statYearly, moldCode);
        // 获取模具指定年度下不同月份的故障次数和故障时长
        Map<Integer, MoldYearlyFaultStatDTO> mttrStatMap = moldRepairRecordManager.doYearlyMoldFaultStat(statYearly, moldCode);
        // 当前月份
        Integer currentMonth = com.sugon.modules.utils.DateUtil.month(null);
        // 今年年份
        int thisYear = DateUtil.thisYear();
        for (int i = 1; i <= 12; i++) {
            // 获取模具当前月份的故障统计累计
            MoldYearlyFaultStatDTO moldYearlyFaultStat = mttrStatMap.get(i);
            // 年份没到或者月份没到的一律默认为0
            if ((statYearly > thisYear) || (statYearly == thisYear && i > currentMonth) || moldYearlyFaultStat == null) {
                chartSeriesModel.add(0.0);
            } else {
                // 获取模具当前月份下的生产时间累计
                double moldProductTime = MapUtil.getDouble(yearlyMoldTimesStat, i, 0.0);
                // 当故障次数和时长都大于0时，计算次数
                int times = moldYearlyFaultStat.getTimes();
                if (times > 0) {
                    chartSeriesModel.add(NumberUtil.div(moldProductTime, times, 2, RoundingMode.HALF_UP));
                } else {
                    chartSeriesModel.add(0.0);
                }
            }
        }
        return chartSeriesModel;
    }

    @Override
    public MoldKpiTarget getType() {
        return MoldKpiTarget.MTBF;
    }

}
